/**
 * 
 */
package com.googlecode.agiletools.backlog.action;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.googlecode.agiletools.backlog.SprintBacklog;
import com.googlecode.agiletools.backlog.SprintBacklogManager;
import com.opensymphony.xwork2.Action;

/**
 * @author Administrator
 * 
 */
public class SprintBacklogAction {

    private SprintBacklogManager sprintBacklogManager;

    private int backlogId;

    private SprintBacklog backlog = new SprintBacklog();

    private JFreeChart chart;

    public String burndown() {
	backlog = sprintBacklogManager.getBacklog(backlogId);

	XYSeries linear = new XYSeries(Integer.valueOf(1), false, false);
	XYSeries effortSeries = new XYSeries(Integer.valueOf(2), false, false);
	String[] symbols = new String[backlog.getLeftEfforts().size()];

	Float averageDay = backlog.getLeftEfforts().get(0) / backlog.getDay();

	for (int i = 0; i < backlog.getLeftEfforts().size(); i++) {
	    effortSeries.add(i, backlog.getLeftEfforts().get(i));
	    linear.add(i, averageDay * (backlog.getDay() - i));
	    symbols[i] = String.valueOf(backlog.getDay() - i);
	}
	XYSeriesCollection xyDataset = new XYSeriesCollection(effortSeries);
	xyDataset.addSeries(linear);

	ValueAxis xAxis = new SymbolAxis("剩余日期", symbols);
	ValueAxis yAxis = new NumberAxis("剩余工作量");

	chart = new JFreeChart("Sprint "
		+ String.valueOf(backlog.getSprintNumber()) + ": "
		+ backlog.getHeadline(), JFreeChart.DEFAULT_TITLE_FONT,
		new XYPlot(xyDataset, xAxis, yAxis, new XYSplineRenderer()),
		false);

	return Action.SUCCESS;
    }

    public String create() {
	return Action.INPUT;
    }

    public String detail() {
	backlog = sprintBacklogManager.getBacklog(backlogId);
	return Action.SUCCESS;
    }

    /**
     * @return the backlog
     */
    public SprintBacklog getBacklog() {
	return backlog;
    }

    /**
     * @return the backlogId
     */
    public int getBacklogId() {
	return backlogId;
    }

    /**
     * @return the chart
     */
    public JFreeChart getChart() {
	return chart;
    }

    public String modify() {
	backlog = sprintBacklogManager.getBacklog(backlogId);
	System.out.println(backlog.getLeftEfforts().size());
	return Action.INPUT;
    }

    public String save() {
	backlog = sprintBacklogManager.saveBacklog(backlog);
	return Action.SUCCESS;
    }

    /**
     * @param backlog
     *            the backlog to set
     */
    public void setBacklog(SprintBacklog backlog) {
	this.backlog = backlog;
    }

    /**
     * @param backlogId
     *            the backlogId to set
     */
    public void setBacklogId(int backlogId) {
	this.backlogId = backlogId;
    }

    /**
     * @param sprintBacklogManager
     *            the sprintBacklogManager to set
     */
    public void setSprintBacklogManager(
	    SprintBacklogManager sprintBacklogManager) {
	this.sprintBacklogManager = sprintBacklogManager;
    }

}
